from lexer.lexer import Lexer
from parse.parser import Parser
from parse.symbol_table.symbol_table import SymbolTable
from generator.generator import Generator
from parse.AST.program import Program

class Complier:
    def __init__(self, inputFileName):
        lexer = Lexer(inputFileName)
        symbolTable = SymbolTable()
        parser = Parser(lexer, symbolTable)
        programAST = parser.program(Program())
        outputFile = self.openOutputFile(inputFileName)
        generator = Generator(programAST, outputFile)
        generator.generateAsm()

    def openOutputFile(self, inputFileName: str):
        if inputFileName.endswith(".c"):
            outputFileName = inputFileName.split(".c")[0] + ".s"
        else:
            outputFileName = inputFileName + ".s"
        file = open(outputFileName, "w")
        return file
